package com.study.mike.spring;

import com.study.mike.spring.ext.MyTypeFilter;
import com.study.mike.spring.service.Abean;
import com.study.mike.spring.service.Bbean;
import com.study.mike.spring.service.CombatService;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.support.GenericApplicationContext;

@Configuration
@ComponentScan(includeFilters = { @Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class) })
public class MikeApplication {

	public static void main(String[] args) {

		/** xml 方式 **************** */
		CombatService cs = null;

//		ApplicationContext clpXmlApplicationContext = new ClassPathXmlApplicationContext("application.xml");
//		cs = clpXmlApplicationContext.getBean(CombatService.class);
//		cs.doInit();
//		cs.combating();

//		ApplicationContext fsXmlApplicationContext = new FileSystemXmlApplicationContext("//Volumes/APFS/Projects/IDEA-study-source-code/study-spring-source/src/main/resources/application.xml");
//		cs = fsXmlApplicationContext.getBean(CombatService.class);
//		cs.doInit();
//		cs.combating();


		/** 通用 方式 **************** */

//		ApplicationContext genXmlApplicationContext = new GenericXmlApplicationContext("file:/Volumes/APFS/Projects/IDEA-study-source-code/study-spring-source/src/main/resources/application.xml");
//		cs = genXmlApplicationContext.getBean(CombatService.class);
//		cs.doInit();
//		cs.combating();

		// 注解的方式
		ApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext("com.study.mike.spring.service");
		Bbean bbean = annotationConfigApplicationContext.getBean(Bbean.class);
		bbean.sayHello();

		System.out.println("------------------------------------------------------");
		GenericApplicationContext genApplicationContext = new GenericApplicationContext();
		new XmlBeanDefinitionReader(genApplicationContext).loadBeanDefinitions("classpath:application.xml");
//		new ClassPathBeanDefinitionScanner(genApplicationContext).scan("com.study.mike.spring.service");
		// 一定要刷新
		genApplicationContext.refresh();
		cs = genApplicationContext.getBean(CombatService.class);
		cs.combating();
		Abean ab = genApplicationContext.getBean(Abean.class);
		ab.doSomething();
	}

	@Bean
	public CombatService getCombatService() {
		return new CombatService(120);
	}
}
